package com.yy.wga.wechat.service.controller;


import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.yy.wga.common.core.dto.Result;
import com.yy.wga.wechat.feign.response.WechatGetAccessTokenResp;
import com.yy.wga.wechat.service.manager.WechatManager;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import org.springframework.web.bind.annotation.*;

/**
 * 微信小程序能力支持
 *
 * @author guochao
 * @since 2024-12-10 22:41:43
 */
@RestController
@RequestMapping(value = "/api/wechat/mp/")
public class WechatMpController {
    /**
     * 服务对象
     */
    @Resource
    private WechatManager wechatManager;

    /**
     * 通过code获取用户信息
     * @return
     */
    @PostMapping("getUserSessionInfo")
    public Result<WxMaJscode2SessionResult> getUserSessionInfo(@RequestParam("code") String code) {
        WxMaJscode2SessionResult result = wechatManager.getUserSessionInfo(code);
        return Result.success(result);
    }

    /**
     * 通过code获取用户信息
     * @return
     */
    @PostMapping("getOfficialOAuth2AccessToken")
    public Result<WxOAuth2AccessToken> getOfficialOAuth2AccessToken(@RequestParam("code") String code) {
        WxOAuth2AccessToken result = wechatManager.getOfficialOAuth2AccessToken(code);
        return Result.success(result);
    }

    /**
     * 获取accessToken
     * @return
     */
    @PostMapping("getAccessToken")
    public Result<WechatGetAccessTokenResp> getAccessToken() {
        WechatGetAccessTokenResp response = wechatManager.getAccessToken();
        return Result.success(response);
    }

    /**
     * 获取视频号accessToken
     * @return
     */
    @PostMapping("getChannelAccessToken")
    public Result<WechatGetAccessTokenResp> getChannelAccessToken() {
        WechatGetAccessTokenResp response = wechatManager.getChannelAccessToken();
        return Result.success(response);
    }

    /**
     * 获取视频号accessToken
     * @return
     */
    @PostMapping("sendSubscribeMsg")
    public Result<Boolean> sendSubscribeMsg(@RequestBody @Valid WxMaSubscribeMessage request) {
        wechatManager.sendSubscribeMsg(request);
        return Result.success(Boolean.TRUE);
    }

}
